package fragments;

import android.app.Activity;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.wenxin.administrator.zhufengfm.R;

import java.util.LinkedList;
import java.util.List;

import adapter.CommentFragmentPagerAdapter;
import fragments.discover.DiscoverAnchorFragment;
import fragments.discover.DiscoverCategoryFragment;
import fragments.discover.DiscoverLiveFragment;
import fragments.discover.DiscoverRatingFragment;
import fragments.discover.DiscoverRecommendFragment;


public class DiscoverFragment extends Fragment implements TabLayout.OnTabSelectedListener{

    public DiscoverFragment() {
    }
    ViewPager pager;
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_discover, null);
       TabLayout tabLayout= (TabLayout) view.findViewById(R.id.discover_tab_bar);
//        TabLayout.Tab tab=tabLayout.newTab().setText("推荐");
//        tabLayout.addTab(tab);
//
//        tab=tabLayout.newTab().setText("分类");
//        tabLayout.addTab(tab);
//
//        tab=tabLayout.newTab().setText("直播");
//        tabLayout.addTab(tab);
//
//        tab=tabLayout.newTab().setText("榜单");
//        tabLayout.addTab(tab);
//
//        tab=tabLayout.newTab().setText("主题");
//        tabLayout.addTab(tab);

        //viewpager加载
        pager= (ViewPager) view.findViewById(R.id.discover_view_pager);
        //创建Fragment集合
        List<BaseFragment> fragments=new LinkedList<BaseFragment>();
        fragments.add(new DiscoverRecommendFragment());
        fragments.add(new DiscoverCategoryFragment());
        fragments.add(new DiscoverLiveFragment());
        fragments.add(new DiscoverRatingFragment());
        fragments.add(new DiscoverAnchorFragment());
        //创建viewpager适配器
        CommentFragmentPagerAdapter pagerAdapter=new CommentFragmentPagerAdapter(getChildFragmentManager(),fragments);
       //给viewpager设置适配器
        pager.setAdapter(pagerAdapter);
        //viewpager滑动与tablayout绑定
       pager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        //tabLayout.setOnTabSelectedListener(this);
        //和viewpager联动,必须重写getPageTitle()方法
        tabLayout.setupWithViewPager(pager);
        return view;
    }

    //当点击tab viewpager跳转到当前页
    public void onTabSelected(TabLayout.Tab tab) {
       pager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }
}
